/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.handlers;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.uml2.uml.InteractionFragment;

import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.AnalyseOwnerCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.SelectPartCommand;
import com.cea.papyrus.diagram.sequence.sedi.edit.command.SetPreferredBoundsHintCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CoverCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CreateGraphicalElementCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.CreateStateInvariantCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.command.diagram.SetBoundsHintCommand;
import com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil;

/**
 * Handles creation of StateInvariants.
 * <p>
 * This handler is used from FrameLayoutPolicy.
 * </p>
 * 
 * @author frankd@ifi.uio.no
 */
public class StateInvariantCreateHandler extends InteractionFragmentCreateHandler {

	public StateInvariantCreateHandler(final EditPolicy hostPolicy) {
		super(hostPolicy);
	}

	@SuppressWarnings("unchecked")
	public Command[] getCreateCommands(final CreateRequest request) {

		boolean isInitialised = initialize(request);
		if (! isInitialised) {
			Debug.log(new Exception("Command initialization error."));
			return null;
		}
		// CreateStateInvariantCommand [UML]
		// Creates the UML element and adds it to the UML interaction
		// When the command is executed, a GraphicalElement will be
		// created in response to the notification. This creation will
		// consume the Bounds hint set previously
		// AnalyseBestCoveredCommand [SeDi]
		// Analyses the diagram and detects the closest lifeline covered
		// by the new fragment. The result is input to the next command:
		// CoverCommand [UML]
		// Sets the fragment's Covered attribute.

		final AnalyseOwnerCommand ownerCmd = new AnalyseOwnerCommand();
		ownerCmd.setDiagram(diagram);
		ownerCmd.setAnyEditPart(interactionEP);
		ownerCmd.setLocation(relativeLoc);

		final SetBoundsHintCommand hintCmd = new SetBoundsHintCommand();
		hintCmd.setHint(new Rectangle(absolutLoc.x, absolutLoc.y, 0, 0));

		final CreateStateInvariantCommand createCmd = 
			new CreateStateInvariantCommand(bestCoveredLifeline, interaction);  // TODO : interaction dont result from previous command
		// glue: namespace

		final CreateGraphicalElementCommand createGECmd = new CreateGraphicalElementCommand();
		createGECmd.setAnyEditPart(interactionEP);

		final SetPreferredBoundsHintCommand coverHint = new SetPreferredBoundsHintCommand();

		final CoverCommand coverCmd = new CoverCommand();

		final SelectPartCommand selectCmd = new SelectPartCommand();
		selectCmd.setAnyEditPart(hostPolicy.getHost().getRoot());
		selectCmd.setSelectForEdit(true);

		final Command glue0 = new Command() {
			public void execute() {
				createCmd.setNamespace(ownerCmd.getOwner());
			}
		};

		final Command glueGE = new Command() {
			@Override
			public void execute() {
				createGECmd.setParentGE(ModelUtil.getGraphElement(ownerCmd
						.getOwner(), diagram));
				createGECmd.setElement(createCmd.getStateInvariant());
			}
		};

		final Command glue1 = new Command() {
			public void execute() {
				InteractionFragment fragment = createCmd.getStateInvariant();
			}
		};

		final Command glue2 = new Command() {
			public void execute() {
				coverHint.setEditPart(interactionEP);
				coverHint.setFragment(createCmd.getStateInvariant());
				coverHint.setCovered(bestCovereds);

				coverCmd.setFragment(createCmd.getStateInvariant());
				coverCmd.setCovered(bestCovereds);

				selectCmd.setTarget(createCmd.getStateInvariant());
			}
		};

		return new Command[] { hintCmd, ownerCmd, glue0, createCmd, glueGE,
				createGECmd, glue1, glue2, coverHint,
				coverCmd, selectCmd };
	}
}